<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：User.class.php
 * @类功能: 用户控制器
 * @开发者: 陈旭林
 * @开发时间： 2017-09-01
 * @版本：version 1.0
 */

namespace app\api\controller;

use app\common\cache\user\IUserCache;
use app\common\controller\ApiBaseController;
use app\common\enum\AppType;
use app\common\enum\IsMarry;
use app\common\enum\IsOverdue;
use app\common\enum\IsPayPwd;
use app\common\enum\IsReal;
use app\common\enum\Source;
use app\common\enum\UserStatus;
use app\common\enum\UserType;
use app\common\ext\IConfig;
use app\common\ext\IDb;
use app\common\ext\IIdentity;
use app\common\ext\ISms;
use app\common\ext\IJuHe;

class User extends ApiBaseController{

    /**
     * @功能：用户注册
     * @开发者：cxl
     */
    public function reg(){
        //接收参数
        $os = self::$oid;
        $user_phone = self::get_data('phone',self::PHONE,"请输入正确的手机号码！");
        $user_pwd = self::get_data('pwd',self::NOT_EMPTY,"请输入密码！");
        $user_verification = self::get_data('verification',self::NOT_EMPTY,"请输入短信验证码！");

        //判断验证码是否正确
        $verify_list = ISms::getCode(AppType::YH,$user_phone);
        if (!in_array($user_verification,$verify_list)) {
            self::set_code(self::WARNING_0001);
            self::set_msg("无效的验证码！");
            self::send();
        }

        //判断手机号是否已经存在
        $user_where['phone'] = $user_phone;
        $user_count = IDb::getInstance("user")->setDbWhere($user_where)->con();
        if($user_count > 0){
            self::set_code(self::WARNING_0002);
            self::set_msg("亲，您输入的手机号已经被注册了！");
            self::send();
        }

        //开始事物出来
        IDb::dbStartTrans();

        //取得系统配置信息
//        $level_id = IConfig::getValue("UserDefaultLevel");

        //添加用户信息
        $user_data['phone'] = $user_phone;
        $user_data['nick'] = get_string_rsa($user_phone, 3, 4) . "_" . rand(1000,9999);
        $user_data['pwd'] = md5(md5($user_pwd));
//        $user_data['level_id'] = $level_id;
        $user_data['source'] = $os;
        $user_data['reg_ip'] = get_client_ip();
        $user_data['reg_time'] = get_date_time();
        $user_id = IDb::getInstance("user")->setDbData($user_data)->add();
        if($user_id === false){
            //回滚事物
            IDb::dbRollback();

            //返回错误
            self::set_code(self::ERR_0003);
            self::set_msg("用户注册失败，请联系客服！");
            self::send();
        }

//        //添加用户账户信息
//        $user_account_data['user_id'] = $user_id;
//        $user_account_data['credit'] = intval(IConfig::getValue("DefaultQuota"));
//        $user_account_id = IDb::getInstance("user")->setDbData($user_account_data)->add();
//        if($user_account_id === false){
//            //回滚事物
//            IDb::dbRollback();
//
//            self::set_code(self::ERR_0004);
//            self::set_msg("用户注册失败，请联系客服！");
//            self::send();
//        }

        //提交事物
        IDb::dbCommit();

        //判断手机号是否已经存在
        $user_info = IUserCache::getInfo($user_id);
        if(empty($user_info)){
            self::set_code(self::WARNING_0001);
            self::set_msg("取得登录用户信息失败！");
            self::send();
        }

        //设置身份证信息
        if(!empty($user_info['real'])){
            $user_info['real'] = get_string_rsa($user_info['real'],1);
        }

        //设置身份证信息
        if(!empty($user_info['identity'])){
            $user_info['identity'] = get_string_rsa($user_info['identity'],5,8);
        }

        //用户密码信息
        unset($user_info['pwd'],$user_info['pay_pwd']);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_values($user_info);
        self::send();
    }

    /**
     * @功能：用户登录
     * @开发者：cxl
     */
    public function login(){
        //接收参数
        $user_phone = self::get_data('phone',self::PHONE,"请输入正确的手机号码！");
        $user_pwd = self::get_data('pwd',self::NOT_EMPTY,"请输入密码！");

        //查询用户自己注册账号
        $user_where['phone'] = $user_phone;
        $user_where['user_type'] = 1;
        $user_info = IDb::getInstance("user")->setDbWhere($user_where)->row();
        if(empty($user_info)){
            //查询合作商录入账号
            $user_where['user_type'] = 2;
            $user_info = IDb::getInstance("user")->setDbWhere($user_where)->row();

            if(empty($user_info)){
                self::set_code(self::WARNING_0001);
                self::set_msg("亲，您输入的手机号未注册！");
                self::send();
            }
        }

        //判断用户是否被加入黑名单
        if($user_info['user_status'] == UserStatus::blacklist){
            self::set_code(self::ERR_CHECK_0006);
            self::set_msg("亲，您的账号已被禁用，请联系客服！");
            self::send();
        }

        //判断密码是否正确
        if(md5(md5($user_pwd)) != $user_info['pwd']){
            self::set_code(self::WARNING_0003);
            self::set_msg("亲，您输入的密码错误！");
            self::send();
        }else{
            //设置身份证信息
            if(!empty($user_info['real'])){
                $user_info['real'] = get_string_rsa($user_info['real'],1);
            }

            //设置身份证信息
            if(!empty($user_info['identity'])){
                $user_info['identity'] = get_string_rsa($user_info['identity'],5,8);
            }

            //用户密码信息
            unset($user_info['pwd'],$user_info['pay_pwd']);
        }

        //修改最后登录时间
        $user_data = null;
        $user_where = null;
        $user_where['user_id'] = $user_info['user_id'];
        $user_data['login_ip'] = get_client_ip();
        $user_data['login_time'] = get_date_time();
        $user_upd = IDb::getInstance("user")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::WARNING_0004);
            self::set_msg("登录失败！");
            self::send();
        }

        //设置数据
        self::set_token('_u',$user_info['user_id']); //设置设备编号

        //清除用户缓存
        IUserCache::deleteInfo($user_info['user_id']);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value(self::SUBSCRIPT_TOKEN,self::get_token());
        self::set_values($user_info);
        self::send();
    }

    /**
     * @功能：用户短信登录
     * @开发者：cxl
     */
    public function sms_login(){
        //接收参数
        $os = self::$oid;
        $user_phone = self::get_data('user_phone',self::PHONE,"请输入正确的手机号码！");
        $user_verification = self::get_data('user_verification',self::NOT_EMPTY,"请输入短信验证码！");

        //判断验证码是否正确
        $verify_list = ISms::getCode(AppType::YH,$user_phone);
        if (!in_array($user_verification,$verify_list)) {
            self::set_code(self::WARNING_0001);
            self::set_msg("亲，您输入的短信验证码错误！");
            self::send();
        }

        //判断手机号是否已经存在
        $user_where['phone'] = $user_phone;
        $user_info = IDb::getInstance("user")->setDbWhere($user_where)->row();
        if($user_info === false){
            self::set_code(self::WARNING_0002);
            self::set_msg("短信登录失败，请联系客服！");
            self::send();
        }

        if(empty($user_info)){
            //查询用户自己注册账号
            $user_where['phone'] = $user_phone;
            $user_where['user_type'] = 1;
            $user_info = IDb::getInstance("user")->setDbWhere($user_where)->row();
            if($user_info === false){
                self::set_code(self::WARNING_0001);
                self::set_msg("短信登录失败，请联系客服！");
                self::send();
            }
        }

        if(empty($user_info)){
            //查询合作商录入账号
            $user_where['user_type'] = 2;
            $user_info = IDb::getInstance("user")->setDbWhere($user_where)->row();

            if($user_info === false){
                self::set_code(self::WARNING_0001);
                self::set_msg("短信登录失败，请联系客服！");
                self::send();
            }
        }

        //判断用户是否存在
        if(empty($user_info)){
            //取得系统配置信息
            $level_id = IConfig::getValue("UserDefaultLevel");

            //添加用户信息
            $user_data['phone'] = $user_phone;
            $user_data['nick'] = get_string_rsa($user_phone, 3, 4) . "_" . rand(1000,9999);
            $user_data['level_id'] = $level_id;
            $user_data['source'] = $os;
            $user_data['user_status'] = UserStatus::normal;
            $user_data['reg_ip'] = get_client_ip();
            $user_data['reg_time'] = get_date_time();
            $user_id = IDb::getInstance("user")->setDbData($user_data)->add();
            if($user_id === false){
                self::set_code(self::WARNING_0003);
                self::set_msg("短信登录失败，请联系客服！");
                self::send();
            }
        }else{
            $user_id = $user_info['user_id'];
        }

        //修改最后登录时间
        $user_data = null;
        $user_where = null;
        $user_where['user_id'] = array("eq",$user_id);
        $user_data['login_ip'] = get_client_ip();
        $user_data['login_time'] = get_date_time();
        $user_upd = IDb::getInstance("user")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::WARNING_0004);
            self::set_msg("登录失败！");
            self::send();
        }

        //清除用户缓存
        IUserCache::deleteInfo($user_id);

        //取得用户信息
        $user_info = IUserCache::getInfo($user_id);

        //判断用户是否被加入黑名单
        if($user_info['user_status'] == UserStatus::blacklist){
            self::set_code(self::ERR_CHECK_0006);
            self::set_msg("亲，您的账号已被禁用，请联系客服！");
            self::send();
        }

        //设置用户信息
        if(!empty($user_info['real'])){ $user_info['real'] = get_string_rsa($user_info['real'],1); }//设置身份证信息
        if(!empty($user_info['identity'])){ $user_info['identity'] = get_string_rsa($user_info['identity'],5,8); }//设置身份证信息

        //用户密码信息
        unset($user_info['pwd'],$user_info['pay_pwd']);

        //设置数据
        $this->set_token('_u',$user_info['user_id']); //设置设备编号

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value(self::SUBSCRIPT_TOKEN,$this->get_token());
        self::set_values($user_info);
        self::send();
    }

    /**
     * @功能：用户退出
     * @开发者：陈旭林
     */
    public function logout(){
        //接收参数
        $device  = $this->get_data('device_no',self::NOT_EMPTY,"设备编号错误！");
        $os = $this->get_data('os',self::NOT_EMPTY,"系统区分错误！");
        $version  = $this->get_data('version',self::NOT_EMPTY,"系统版本错误！");

        //检测保存设备ID
        if (empty($device) || strlen($device) != 32) {
            self::set_code(self::ERR_0001);
            self::set_msg("设备编号错误！");
            self::send();
        }

        //判断系统区分是否正确
        if(!in_array($os,array(Source::Android,Source::Ios))){
            self::set_code(self::ERR_0002);
            self::set_msg("系统区分错误！");
            self::send();
        }

        //设置数据
        $this->set_token(self::USER,null); //设置用户编号
        $this->set_token(self::DEVICE,$device); //设置设备编号
        $this->set_token(self::OS,$os); //设置系统区分
        $this->set_token(self::VERSION,$version); //设置版本信息

        //返回参数
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value(self::SUBSCRIPT_TOKEN,$this->get_token());
        self::send();
    }

    /**
     * @功能：取得登录用户信息
     * @开发者：cxl
     */
    public function login_info(){
        //接收参数
        $user_info = self::$info;

        //设置身份证信息
//        if(!empty($user_info['real'])){
//            $user_info['real'] = get_string_rsa($user_info['real'],1);
//        }

//        设置身份证信息
//        if(!empty($user_info['identity'])){
//            $user_info['identity'] = get_string_rsa($user_info['identity'],5,8);
//        }

//        //设置配偶身份证信息
//        if(!empty($user_info['couple_identity'])){
//            $user_info['couple_identity'] = get_string_rsa($user_info['couple_identity'],5,8);
//        }

        //用户密码信息
        unset($user_info['pwd'],$user_info['pay_pwd']);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_values($user_info);
        self::send();
    }

    /**
     * @功能：设置用户昵称
     * @开发者：cxl
     */
    public function set_field(){
        //接收参数
        $user_id = self::$uid;
        $field  = self::get_data("field",self::NOT_EMPTY,"请输入修改字段！");
        $value  = self::get_data("value");

        //判断是否输入
        if(empty($value)){
            self::set_code(self::WARNING_0001);
            self::set_msg("请输入用户信息！");
            self::send();
        }

        //判断字段是否正确
        $field = strtolower($field);
        $field_array = array("nick","avatar");
        if(!in_array($field,$field_array)){
            self::set_code(self::WARNING_0002);
            self::set_msg("修改用户信息错误，请联系客服！");
            self::send();
        }

        //设置用户昵称
        $user_where['user_id'] = $user_id;
        $user_data[$field] = $value;
        $user_upd = IDb::getInstance("user")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::WARNING_0003);
            self::set_msg("修改用户昵称失败！");
            self::send();
        }

        //删除缓存
        IUserCache::deleteInfo($user_id);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：修改密码
     * @开发者：cxl
     */
    public function upd_pwd(){
        //接收参数
        $user_id = self::$uid;
        $user_info = self::$info;
        $user_verification = self::get_data('user_verification',self::NOT_EMPTY,"请输入短信验证码！");
        $new_pwd = self::get_data('new_pwd',self::NOT_EMPTY,"请输入新密码！");

        //判断验证码是否正确
        $verify_list = ISms::getCode(AppType::YH,$user_info['phone']);
        if (!in_array($user_verification,$verify_list)) {
            self::set_code(self::WARNING_0001);
            self::set_msg("亲，您输入的短信验证码错误！");
            self::send();
        }

        //整理用户信息
        $user_where['user_id'] = $user_id;
        $user_data['pwd'] = md5(md5($new_pwd));
        $user_upd = IDb::getInstance("user")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::ERR_0002);
            self::set_msg("修改用户密码失败，请联系客服！");
            self::send();
        }

        //删除缓存
        IUserCache::deleteInfo($user_id);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：找回密码
     * @开发者：cxl
     */
    public function find_pwd(){
        //接收参数
        $user_phone = self::get_data('user_phone',self::NOT_EMPTY,"请输入电话号码！");
        $verification = self::get_data('verification',self::NOT_EMPTY,"请输入验证码！");
        $new_pwd = self::get_data('new_pwd',self::NOT_EMPTY,"请输入新密码！");

        //判断验证码是否正确
        $verify_list = ISms::getCode(AppType::YH,$user_phone);
        if (!in_array($verification,$verify_list)) {
            $this->set_code(self::WARNING_0001);
            $this->set_msg("亲，您输入的短信验证码错误！");
            $this->send();
        }

        //判断手机号是否已经存在
        $user_where = null;
        $user_where['phone'] = array("eq",$user_phone);
        $user_info = IDb::getInstance("user")->setDbFiled("user_id")->setDbWhere($user_where)->row();
        if(empty($user_info['user_id'])){
            self::set_code(self::WARNING_0002);
            self::set_msg("该手机号尚未注册！");
            self::send();
        }

        //整理用户信息
        $user_where = null;
        $user_data = null;
        $user_where['user_id'] = $user_info['user_id'];
        $user_data['pwd'] = md5(md5($new_pwd));
        $user_upd = IDb::getInstance("user")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::ERR_0001);
            self::set_msg("修改用户密码失败，请联系客服！");
            self::send();
        }

        //删除缓存
        IUserCache::deleteInfo($user_info['user_id']);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：实名验证接口
     * @开发者：cxl
     */
    public function set_real(){
        //接收参数
        $user_id = self::$uid;
        $user_info = self::$info;
        $real = self::get_data('real',self::NOT_EMPTY,"请输入用户真实姓名！");
        $identity = self::get_data('identity',self::NOT_EMPTY,"请输入用户身份证号！");

        //设置身份证信息
        $real_temp = get_string_rsa($real,0,1);
        $identity_temp = get_string_rsa($identity,5,8);

        //判断用户是否已经实名
        if($user_info['is_real'] == IsReal::yes){
            //设置身份证信息
            if(!empty($user_info['real'])){
                $real_temp = get_string_rsa($user_info['real'],1);
            }

            //设置身份证信息
            if(!empty($user_info['identity'])){
                $identity_temp = get_string_rsa($user_info['identity'],5,8);
            }

            //操作成功
            self::set_code(self::WARNING_0002);
            self::set_msg("用户已经实名验证了，不能修改实名信息！");
            self::set_value("real",$real_temp);
            self::set_value("identity",$identity_temp);
            self::send();
        }

        //判断实名是否通过
//        $real_info = IJuHe::real($real,$identity);
//        if($real_info === false){
//            self::set_code(self::WARNING_0003);
//            self::set_msg("用户实名失败！");
//            self::send();
//        }

        //取得身份证自带信息
        $user_data = null;
        $identity_info = IIdentity::getInfo($identity);
        if(!empty($identity_info)){
            $user_data['sex'] = $identity_info['sex'];
            $user_data['age'] = $identity_info['age'];
            $user_data['birthday'] = $identity_info['date'];
        }

        //修改用户实名信息
        $user_where = null;
        $user_where['user_id'] = array("eq",$user_id);
        $user_data['nick'] = $real;
        $user_data['real'] = $real;
        $user_data['identity'] = $identity;
        $user_data['is_real'] = IsReal::not;
        $user_data['real_time'] = get_date_time();
        $user_upd = IDb::getInstance("user")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::WARNING_0004);
            self::set_msg("修改用户昵称失败！");
            self::send();
        }

        //删除缓存
        IUserCache::deleteInfo($user_id);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("real",$real_temp);
        self::set_value("identity",$identity_temp);
        self::send();
    }

    /**
     * @功能：设置支付密码
     * @开发者：cxl
     */
    public function set_pay_pwd(){
        //接收参数
        $user_id = self::$uid;
        $pay_pwd  = self::get_data("pay_pwd",self::NOT_EMPTY,"请输入支付密码！");

        //取得用户账户信息
        $user_where['user_id'] = $user_id;
        $user_data['pay_pwd'] = md5(md5($pay_pwd));
        $user_data['is_pay_pwd'] = IsPayPwd::Yes;
        $user_upd = IDb::getInstance("user")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::WARNING_0002);
            self::set_msg("设置支付密码失败！");
            self::send();
        }

        //删除缓存
        IUserCache::deleteInfo($user_id);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：设置支付密码
     * @开发者：cxl
     */
    public function upd_pay_pwd(){
        //接收参数
        $user_id = self::$uid;
        $user_info = self::$info;
        $old_pay_pwd  = self::get_data("old_pay_pwd",self::NOT_EMPTY,"请输入旧支付密码！");
        $new_pay_pwd  = self::get_data("new_pay_pwd",self::NOT_EMPTY,"请输入新支付密码！");

        //判断用户是否设置支付密码
        if($user_info['pay_pwd'] != md5(md5($old_pay_pwd))){
            self::set_code(self::WARNING_0001);
            self::set_msg("您输入的旧支付密码错误！");
            self::send();
        }

        //取得用户账户信息
        $user_where['user_id'] = $user_id;
        $user_data['pay_pwd'] = md5(md5($new_pay_pwd));
        $user_data['is_pay_pwd'] = IsPayPwd::Yes;
        $user_upd = IDb::getInstance("user")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::WARNING_0002);
            self::set_msg("修改支付密码失败！");
            self::send();
        }

        //删除缓存
        IUserCache::deleteInfo($user_id);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：找回支付密码-修改密码
     * @开发者：陈旭林
     */
    public function find_pay_pwd(){
        //接收参数
        $user_id = self::$uid;
        $user_verify = self::get_data('user_verify', self::NOT_EMPTY,"请输入验证码！");
        $pay_pwd  = self::get_data('pay_pwd',self::NOT_EMPTY,"请输入新密码！");

        //取得用户手机号
        $user_phone = self::$info['phone'];

        //判断验证码是否正确
        $verify_list = ISms::getCode(AppType::YH,$user_phone);
        if (!in_array($user_verify,$verify_list)) {
            self::set_code(self::WARNING_0001);
            self::set_msg("亲，您输入的短信验证码错误！");
            self::send();
        }

        //取得用户账户信息
        $user_where['user_id'] = $user_id;
        $user_data['pay_pwd'] = md5(md5($pay_pwd));
        $user_data['is_pay_pwd'] = IsPayPwd::Yes;
        $user_upd = IDb::getInstance("user")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::WARNING_0002);
            self::set_msg("找回支付密码失败！");
            self::send();
        }

        //删除缓存
        IUserCache::deleteInfo($user_id);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：设置用户基本信息
     * @开发者：cxl
     */
    public function info(){
        //接收参数
        $user_id = self::$uid;
        $user_info = self::$info;
        $real = self::get_param('real',true,self::NOT_EMPTY,'姓名');
        $identity = self::get_param('identity',true,self::IDENTITY,'身份证号');
        $identity_front_url = self::get_data('identity_front_url');
        $identity_back_url = self::get_data('identity_back_url');
//        $sex = self::get_data('sex');
//        $age = self::get_data('age');
        $birth_address = self::get_data('birth_address');

        //判断用户是否通过是否 TODO 实名
//        if($user_info['is_real'] != IsReal::yes){
            //判断实名是否通过
//            $real_info = IJuHe::real($real,$identity);
//            if($real_info === false){
//                self::set_code(self::WARNING_0001);
//                self::set_msg("用户实名失败！");
//                self::send();
//            }
//
//            //设置用户名称及身份证号码
//            $user_data['real'] = $real;
//            $user_data['identity'] = $identity;
//            $user_data['is_real'] = IsReal::yes;
//
//            $identity_info = IIdentity::getInfo($identity);
//            if(is_array($identity_info)){
//                $user_data['sex'] = $identity_info['sex'];
//                $user_data['age'] = $identity_info['age'];
//            }

//        }

        //设置用户名称及身份证号码
        $user_data['real'] = $real;
        $user_data['identity'] = $identity;
        $user_data['birth_address'] = $birth_address;
        if(!empty($identity_front_url)) $user_data['identity_front_url'] = $identity_front_url;
        if(!empty($identity_back_url)) $user_data['identity_back_url'] = $identity_back_url;

        $identity_info = IIdentity::getInfo($identity);
        if(is_array($identity_info)){
            $user_data['sex'] = $identity_info['sex'];
            $user_data['age'] = $identity_info['age'];
        }

        // 如未上传身份证正反面和户籍，则可修改
//        if(empty($user_info['birth_address']) && !empty($birth_address)) $user_data['birth_address'] = $birth_address;
//        if(empty($user_info['identity_front_url']) && !empty($identity_front_url)) $user_data['identity_front_url'] = $identity_front_url;
//        if(empty($user_info['identity_back_url']) && !empty($identity_back_url)) $user_data['identity_back_url'] = $identity_back_url;

        //判断是否有需要修改信息
        if(empty($user_data)){
            //操作成功
            self::set_code(self::SUCCESS);
            self::set_msg("成功！");
            self::set_values($user_info);
            self::send();
        }

        //设置用户昵称
        $user_where['user_id'] = $user_id;
        $user_upd = IDb::getInstance("user")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::WARNING_0002);
            self::set_msg("修改用户基础信息失败！");
            self::send();
        }

        //清除缓存
        IUserCache::deleteInfo($user_id);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：设置用户其它信息
     * @开发者：gys
     */
    public function info_other(){
        //接收参数
        $user_id = self::$uid;
        $is_marry = self::get_data('is_marry',self::INT,'请选择婚姻状况');
        $couple_name = self::get_data('couple_name');
        $couple_phone = self::get_data('couple_phone');
        $education = self::get_data('education',self::INT,'请选择文化程度');
        $loan_phone = self::get_data('loan_phone',self::NOT_EMPTY,'手机号码不能为空');
        self::check_data($loan_phone,self::PHONE,'手机号码',true);
        $domicile_address = self::get_data('domicile_address');
        $occupation = self::get_data('occupation',self::INT,'请选择职业身份');
        $monthly_income = self::get_data('monthly_income',self::NOT_EMPTY,'月均收入不能为空');

        if($couple_phone != ''){
            self::check_data($couple_phone,self::PHONE,'配偶手机号码',true);
        }

        if($is_marry == IsMarry::married){
            //已婚
            $user_data['couple_name'] = $couple_name;
            $user_data['couple_phone'] = $couple_phone;
        }else if($is_marry == IsMarry::divorce){
            //离异
            $user_data['couple_name'] = ['exp','null'];
            $user_data['couple_phone'] = ['exp','null'];
        }else{
            //未婚
            $is_marry = IsMarry::unmarried;
            $user_data['couple_name'] = ['exp','null'];
            $user_data['couple_phone'] = ['exp','null'];
        }

        //设置用户信息
        $user_data['is_marry'] = $is_marry;
        $user_data['education'] = $education;
        $user_data['loan_phone'] = $loan_phone;
        $user_data['domicile_address'] = $domicile_address;
        $user_data['occupation'] = $occupation;
        $user_data['monthly_income'] = $monthly_income;

        $user_upd = IDb::getInstance("user")->setDbWhere(['user_id'=>$user_id])->setDbData($user_data)->upd();
        if($user_upd === false){
            self::set_code(self::WARNING_0002);
            self::set_msg("添加客户信息失败！");
            self::send();
        }

        //清除缓存
        IUserCache::deleteInfo($user_id);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：设置用户资产基本信息
     * @开发者：gys
     */
    public function info_asset(){
        //接收参数
        $user_id = self::$uid;
        $is_company = self::get_data('is_company',self::NOT_EMPTY,'是否有公司不能为空');
        $is_house = self::get_data('is_house',self::NOT_EMPTY,'是否有房不能为空');
        $is_credit_card = self::get_data('is_credit_card',self::NOT_EMPTY,'是否有信用卡不能为空');
        $is_social = self::get_data('is_social',self::NOT_EMPTY,'是否有社保不能为空');
        $is_accumulation_fund = self::get_data('is_accumulation_fund',self::NOT_EMPTY,'是否有公积金不能为空');
        $is_overdue = self::get_data('is_overdue',self::NOT_EMPTY,'是否有逾期不能为空');
//        $is_white = self::get_data('is_white');
        $is_court = self::get_data('is_court',self::NOT_EMPTY,'是否有法院执行记录不能为空');
        $is_policy = self::get_data('is_policy',self::NOT_EMPTY,'是否有保单不能为空');
        $is_car = self::get_data('is_car',self::NOT_EMPTY,'是否有车不能为空');
        $is_loan = self::get_data('is_loan',self::NOT_EMPTY,'是否有贷款不能为空');

        //设置用户信息
        $user_where['user_id'] = $user_id;
        $user_data['is_company'] = $is_company;
        $user_data['is_house'] = $is_house;
        $user_data['is_credit_card'] = $is_credit_card;
        $user_data['is_social'] = $is_social;
        $user_data['is_accumulation_fund'] = $is_accumulation_fund;
//        $user_data['is_white'] = $is_white;
        $user_data['is_overdue'] = $is_overdue;
        $user_data['is_court'] = $is_court;
        $user_data['is_policy'] = $is_policy;
        $user_data['is_car'] = $is_car;
        $user_data['is_loan'] = $is_loan;

        IDb::dbStartTrans();

        //同步个人信息
        $result = \app\common\ext\User::syncUserInfoToMaterial($user_id,$user_data);
        if($result === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0001);
            self::set_msg(\app\common\ext\User::getError());
            self::send();
        }

        IDb::dbCommit();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：设置逾期信息
     * @开发者：cxl
     */
    public function overdue_info(){
        $user_id = self::$uid;

        $data['half_year'] = [];
        $data['one_year'] = [];
        $data['three_year'] = [];
        $data['continuity'] = [];

        $data['half_year']['is_overdue'] = self::get_data('half_year',self::NOT_EMPTY,"请选择最近半年是否逾期！");
        $data['one_year']['is_overdue'] = self::get_data('one_year',self::NOT_EMPTY,"请选择最近一年是否逾期！");
        $data['three_year']['is_overdue'] = self::get_data('three_year',self::NOT_EMPTY,"请选择最近三年是否逾期");
        $data['continuity']['is_overdue'] = self::get_data('continuity',self::NOT_EMPTY,"请选择是否连续逾期");

        $data['half_year']['is_overdue'] = $data['half_year']['is_overdue'] == IsOverdue::Yes ? IsOverdue::Yes : IsOverdue::No;
        $data['one_year']['is_overdue'] = $data['one_year']['is_overdue'] == IsOverdue::Yes ? IsOverdue::Yes : IsOverdue::No;
        $data['three_year']['is_overdue'] = $data['three_year']['is_overdue'] == IsOverdue::Yes ? IsOverdue::Yes : IsOverdue::No;
        $data['continuity']['is_overdue'] = $data['continuity']['is_overdue'] == IsOverdue::Yes ? IsOverdue::Yes : IsOverdue::No;

        $data['half_year']['less_30'] = intval(self::get_data('half_year_less_30'));
        $data['half_year']['over_30'] = intval(self::get_data('half_year_over_30'));
        $data['half_year']['over_60'] = intval(self::get_data('half_year_over_60'));
        $data['half_year']['over_90'] = intval(self::get_data('half_year_over_90'));

        $data['one_year']['less_30'] = intval(self::get_data('one_year_less_30'));
        $data['one_year']['over_30'] = intval(self::get_data('one_year_over_30'));
        $data['one_year']['over_60'] = intval(self::get_data('one_year_over_60'));
        $data['one_year']['over_90'] = intval(self::get_data('one_year_over_90'));

        $data['three_year']['less_30'] = intval(self::get_data('three_year_less_30'));
        $data['three_year']['over_30'] = intval(self::get_data('three_year_over_30'));
        $data['three_year']['over_60'] = intval(self::get_data('three_year_over_60'));
        $data['three_year']['over_90'] = intval(self::get_data('three_year_over_90'));

        $data['continuity']['less_30'] = intval(self::get_data('continuity_less_30'));
        $data['continuity']['over_30'] = intval(self::get_data('continuity_over_30'));
        $data['continuity']['over_60'] = intval(self::get_data('continuity_over_60'));
        $data['continuity']['over_90'] = intval(self::get_data('continuity_over_90'));

        $user_where['user_id'] = $user_id;
        $user_data['overdue_info'] = json_encode($data);

        IDb::dbStartTrans();
        $user_upd = IDb::getInstance("user")->setDbWhere($user_where)->setDbData($user_data)->upd();
        if($user_upd === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0002);
            self::set_msg("修改用户基础信息失败！");
            self::send();
        }

        //同步个人信息
        $result = \app\common\ext\User::syncMaterialToUserInfo($user_id,\app\common\ext\User::MATERIAL_OVERDUE);
        if($result === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0003);
            self::set_msg("同步逾期信息失败！");
            self::send();
        }

        IDb::dbCommit();

        //清除缓存
        IUserCache::deleteInfo($user_id);

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：获取逾期信息
     * @开发者：cxl
     */
    public function get_overdue_info(){
        $user_id = self::$uid;

        $overdue_info = IUserCache::getInfo($user_id,'overdue_info');
        if(empty($overdue_info)){
            $overdue_info = [];
        }else{
            $overdue_info = json_decode($overdue_info);
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_value('overdue_info',$overdue_info);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：我的提问信息
     * @开发者：cxl
     */
    public function question(){
        //接收参数
        $user_id = self::$uid;
        $bottom_id  = self::get_data('bottom_id');

        //查询记录条数
        $question_where['user_id'] = $user_id;
        if(!empty($bottom_id)) $question_where['question_id'] = array("lt",$bottom_id);
        $question_list = IDb::getInstance("question")
            ->setDbFiled("question_id,question_type,question_content,reply_content,create_time,reply_time")
            ->setDbWhere($question_where)
            ->setDbOrder("question_id desc")
            ->setDbLimit(10)
            ->sel();

        //判断数据是否存在
        if(empty($question_list)){
            $question_list = array();
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("question_list",$question_list);
        self::send();
    }
}
